﻿using System.Threading;
using DBServer.Selenium.Extenium.Exception;
using DBServer.Selenium.Extenium.Path;
using Selenium;
using System;

namespace DBServer.Selenium.Extenium.Fixtures
{
    public class YesNoDialogFixture : DialogFixture<YesNoDialogFixture>
    {
        private const string Name = "window.Ext";

        public YesNoDialogFixture(ISelenium driver)
            : base(driver)
        {
        }
      
        public void Yes()
        {
            RequireVisible();

            //Retorna o índice do botão OK
            var command =
                        "function getOKButton()" +
                        "{" +
                        "   var col = window.Ext.MessageBox.getDialog().buttons;" +
                        "   for(i=0; i < col.length; i++)" +
                        "   {" + 
                        "       var btn = col[i];" +
                        "       if(btn.text == \"Sim\")" +
                        "       {" +
                        "	        return i;" +
                        "       }" +
                        "   }" +
                        "   return -1; "                         +
                        "}" +
                        "getOKButton();";

            var index = Int32.Parse(SafeExecDialog(command));

            if(index == -1)
            {
                throw new ExteniumException("The Yes button was not found in the dialog.");
            }

            SafeExecDialog("window.Ext.MessageBox.getDialog().buttons[" + index + "].btnEl.dom.click();");            
        }

        public void No()
        {
            RequireVisible();

            //Retorna o índice do botão OK
            var command =
                        "function getOKButton()" +
                        "{" +
                        "   var col = window.Ext.MessageBox.getDialog().buttons;" +
                        "   for(i=0; i < col.length; i++)" +
                        "   {" +
                        "       var btn = col[i];" +
                        "       if(btn.text == \"Cancel\")" +
                        "       {" +
                        "	        return i;" +
                        "       }" +
                        "   }" +
                        "}" +
                        "getCancelButton();";

            var index = Int32.Parse(SafeExecDialog(command));

            if (index == -1)
            {
                throw new ExteniumException("The No button was not found in the dialog.");
            }

            SafeExecDialog("window.Ext.MessageBox.getDialog().buttons[" + index + "].btnEl.dom.click();");         
        }
    }
}
